package com.gee.lowui;

import javax.microedition.lcdui.*;

public class Tab extends GControl
{
	private Button[] aryBut;

	private int stepX;

	private Image[] imgTab;

	public int rectHieght = 160;

	public int[] rectClip = null;

	public Tab(Image[] img, String[] s, int width)
	{
		if (s != null)
		{
			anchor = Graphics.TOP | Graphics.LEFT;
			imgTab = img;
			aryBut = new Button[s.length];
			controlW = width;
			controlH = img[0].getHeight();
			stepX = (controlW - imgTab[0].getWidth() - 20) / (s.length - 1);
			setString(s);
		}
	}

	public void setString(String[] s)
	{
		if (s != null)
		{
			aryBut = null;
			aryBut = new Button[s.length];
			if (s.length > 1)
			{
				stepX = (controlW - imgTab[0].getWidth() - 20) / (s.length - 1);
			}
			else
			{
				stepX = 0;
			}
			for (int i = 0; i < s.length; i++)
			{
				aryBut[i] = new Button(imgTab, s[i]);
				aryBut[i].setAnchor(Graphics.TOP | Graphics.LEFT);
				aryBut[i].setAction(false);
			}
			index = 0;
			if (aryBut.length > index)
			{
				aryBut[index].setAction(true);
				controlString = aryBut[index].controlString;
			}
			setPostion(x, y);
		}
	}

	public void setPostion(int ex, int ey)
	{
		x = ex;
		y = ey;
		initializePostion();
		for (int i = 0; i < aryBut.length; i++)
		{
			aryBut[i].setPostion(controlX + stepX * i, controlY);
		}
	}

	public void moveIndex(int step)
	{
		index += step;
		if (index < 0)
		{
			index = aryBut.length - 1;
		}
		else if (index > aryBut.length - 1)
		{
			index = 0;
		}
		for (int i = 0; i < aryBut.length; i++)
		{
			aryBut[i].setAction(false);
		}
		if (aryBut.length > index)
		{
			aryBut[index].setAction(true);
			controlString = aryBut[index].controlString;
		}
	}

	public void drawControl(Graphics g)
	{
		g.setFont(fontstyle);
		for (int i = aryBut.length - 1; i > index; i--)
		{
			aryBut[i].setAction(false);
			aryBut[i].drawControl(g);
		}
		for (int i = 0; i < index && index < aryBut.length; i++)
		{
			aryBut[i].setAction(false);
			aryBut[i].drawControl(g);
		}
		if (aryBut.length > index)
		{
			aryBut[index].setAction(true);
			aryBut[index].drawControl(g);
			g.setColor(0x290303);
			g.fillRect(aryBut[index].x, controlY + controlH, aryBut[index].controlW, 2);
		}
		g.setColor(0x290303);
		if (rectClip == null)
		{
			g.fillRect(controlX, controlY + controlH, controlW, rectHieght);
		}
		else
		{	
		
			g.fillRect(controlX, controlY + controlH, controlW, rectClip[1]);
			g.fillRect(controlX, controlY + controlH + rectClip[1], rectClip[0], rectClip[3]);
			g.fillRect(controlX + rectClip[0] + rectClip[2], controlY + controlH + rectClip[1], controlW - rectClip[0] - rectClip[2], rectClip[3]);
			g.fillRect(controlX, controlY + controlH + rectClip[1] + rectClip[3], controlW, rectHieght - rectClip[1] - rectClip[3]);
//			g.setColor(0x908E7E);
//			g.drawRect(controlX + rectClip[0], controlY + controlH + rectClip[1], rectClip[2], rectClip[3]);
			int[] color = { 0x000000, 0x67470C, 0x000000, 0x000000, 0x150202, 0x150202 };
			GUtil.drawImageRect(g, controlX+rectClip[0], controlY + controlH + rectClip[1], rectClip[2], rectClip[3], color, false, false);
		}
		g.setColor(0x716E58);
		g.drawRect(controlX, controlY + controlH, controlW - 2, rectHieght);
		g.setColor(0x000000);
		g.drawRect(controlX + 1, controlY + controlH + 1, controlW - 4, rectHieght - 2);
		if (aryBut.length > index)
		{
			g.setColor(0x290303);
			g.fillRect(aryBut[index].x + 1, controlY + controlH, aryBut[index].controlW - 3, 2);
		}
	}

	public int getSize()
	{
		return aryBut.length;
	}

	public int getControlType()
	{
		return GControl.CONTROL_TAB;
	}

	public int getWidth()
	{
		return controlW;
	}
}
